package View.setFolderDialog;

import View.MainFrame;

import javax.sound.midi.VoiceStatus;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

public class SetFolderDialog implements ActionListener {
    Window parentFrame;

    private JFileChooser openFileChooser;

    boolean closeFlag;


    String path;

    private SetFolderDialog(Window window, String path) {

        parentFrame = window;
        if (openFileChooser == null) {
            openFileChooser = new JFileChooser();
        }
        openFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        if (path == null){
            try{
                openFileChooser.setCurrentDirectory(new File(".").getCanonicalFile());
            }catch (IOException e){
                e.printStackTrace();
            }
            path = "." ;
        }

        else{
            try{
                openFileChooser.setCurrentDirectory(new File(path).getCanonicalFile());
            }catch (IOException e){
                e.printStackTrace();
            }

            this.path = path;
        }



        //this.parentFrame = parentFrame;
        //jtfPath.setText(path);
        //setTitle(title);


        /*jbtnOK.addActionListener(this);
        jbtnOK.setActionCommand(OK_COMMAND);

        jbtnCancel.addActionListener(this);
        jbtnCancel.setActionCommand(CANCEL_COMMAND);

        jbtnOpener.addActionListener(this);
        jbtnOpener.setActionCommand(OPENER_COMMAND);

        GridBagLayout gb = new GridBagLayout();
        setLayout(gb);

        JLabel jlProbability = new JLabel("Set path: ");
        gb.setConstraints(jlProbability, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(10, 10, 0, 0), 0, 0));
        add(jlProbability);
        gb.setConstraints(jtfPath, new GridBagConstraints(0, 1, 3, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 10, 0), 0, 0));
        add(jtfPath);
        //gb.setConstraints(jbtnOpener, new GridBagConstraints(3, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 10, 0), 0, 0));
        //add(jbtnOpener);

        gb.setConstraints(jbtnOK, new GridBagConstraints(0, 2, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 0, 10), 0, 0));
        add(jbtnOK);
        gb.setConstraints(jbtnCancel, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 0, 10), 0, 0));
        add(jbtnCancel);*/
    }

    public static String runDialog(Window window, String path) {
        SetFolderDialog dialog = new SetFolderDialog(window, path);
        dialog.chooseString();
        return dialog.getPath();
    }

    public void actionPerformed(ActionEvent e) {
        /*if (e.getActionCommand().equals(OK_COMMAND)) {
            path = jtfPath.getText();
            closeFlag = true;
            setVisible(false);

        }
        if (e.getActionCommand().equals(CANCEL_COMMAND)) {
            setVisible(false);
        }
        if (e.getActionCommand().equals(OPENER_COMMAND)) {

        }*/

    }


    private void chooseString()
    {
        int returnVal = openFileChooser.showOpenDialog(parentFrame);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            path = openFileChooser.getSelectedFile().getPath();
        }
    }
    public String getPath() {
        return path;
    }
}
